<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Upload extends ADMIN_Controller  {
	
	function __construct(){
		parent::__construct();
		$this->load->helper("url");
		$this->load->helper('form');
		$this->load->helper("file");
		$this->load->model("showlist");
	}
	
	public $batch = array (
			0 => "Batch",
			2004 => "2004",
			2005 => "2005",
			2006 => "2006",
			2007 => "2007",
			2008 => "2008",
			2009 => "2009",
			2010 => "2010",
			2011 => "2011",
			2012 => "2012",
			2013 => "2013"
	);
	
	public function singleupload() {
		
		$id = $this->showlist->add_member();
		redirect('admin/name/view/'.$id);
	}
	
	public function index()
	{
		 	$deptarray = array();
		 	foreach($this->showlist->retrieveDept() as $object){
		 		$deptarray[$object->deptId] = $object->deptName;
		 	}
		 	$this->data["dept"] = $deptarray;
			$this->data["error"] = '';
			$this->data["success"] = '';
			$this->render_page('admin_upload');
	}
	
	function do_upload()
	{
		$config['upload_path'] = realpath(dirname(dirname(dirname(dirname(__FILE__)))).'/assets/uploads/csv/');
		$config['allowed_types'] = 'csv';
		$config['file_name'] = 'tempcsv';
		$this->load->library('upload', $config);
		$this->data["error"] = '';
		$this->data["success"] = '';
	
		if ( ! $this->upload->do_upload())
		{
			$this->data["error"] = 'Your File can not be uploaded, please try using a CSV file';
			$this->render_page('admin_upload');
		}
		else
		{
			$data = array('upload_data' => $this->upload->data());
			$file = fopen(asset_url()."uploads/csv/tempcsv.csv","r");
			$record = array();
			$header = fgetcsv($file);
			while (($row = fgetcsv($file)) !== false) {
				$record[] = array_combine($header, $row);
			}
			$this->db->insert_batch('members', $record);
			fclose($file);
			delete_files(asset_url()."uploads/csv/");
			$this->data["success"] = 'Your File Has been Successfully Uploaded';
			$this->render_page('admin_upload');
		}
		
	
	}
	
	
}

/* End of file index.php */
/* Location: ./application/controllers/welcome.php */